<?php 
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
include_once("includes/db.inc");
include_once("includes/functions.inc");

session_start();
include_once("includes/session.inc");

ControleAcces("calendrier.php", null, null, $user_id, session_id());
if(!$user_id) 
	exit;

include ("includes/header.inc");
$user = GetUserLogin();
put_log(6,_("Entr&eacute;e dans calendrier"),$user);	

// Si les variables de l'URL sont vides, on choisis le mois et l'ann&eacute;e en cours
$day = '';
if (isset($_GET['date_to']))
	$date_to = $_GET['date_to'];
else
	$date_to= '';	
	
if (!empty($date_to)) {
	$d = explode("-", $date_to);
	if (count($d) >= 3) {
		$year = trim($d[0]);
		$mon  = trim($d[1]);
		$day  = trim($d[2]);
	}
} 
if (empty($day)) {
	$day = "1";
	if (isset($_GET['mon']))
		$mon = $_GET['mon'];
	if(empty($mon))
		$mon = date("n");
	if (isset($_GET['year']))
		$year = $_GET['year'];	
	if(empty($year))
		$year = date("Y");	
	$date_to = "$year-$mon-$day";
}
?>
<head>
<style>
.text10{
	font-family: verdana;
	font-size: 10px;
}
.text10bold{
	font-family: verdana;
	font-size: 10px;
	font-weight: bold;
}
</style>
</head>
<br>	
<table border="0" cellspacing="0" cellpadding="0" align="center">
<tr class="text10bold" align="center">
<td>
<?php 
$mois = array(1 =>_('Janvier'), _('F&eacute;vrier'), _('Mars'), _('Avril'), _('Mai'), _('Juin'), _('Juillet'), _('Ao&ucirc;t'), _('Septembre'), _('Octobre'), _('Novembre'), _('D&eacute;cembre'));
echo $mois[$mon].' '.$year;
?>
</td>
</tr>
</table>
<table  border="1" bgcolor=FFFFCC bordercolorlight="#002B3D" bordercolordark="#0095B0" rules="none" cellspacing="0" cellpadding="0" align="center">
<tr class="text10bold" align="center" bgcolor=CCFFCC>
<td><?php echo _("Dimanche"); ?></td>
<td><?php echo _("Lundi"); ?></td>
<td><?php echo _("Mardi"); ?></td>
<td><?php echo _("Mercredi"); ?></td>
<td><?php echo _("Jeudi"); ?></td>
<td><?php echo _("Vendredi"); ?></td>
<td><?php echo _("Samedi"); ?></td>
</tr>
<?php 
$tempDate = getdate(mktime(0,0,0,$mon,1,$year));
$firstwday= $tempDate["wday"];

$lastday = date("t", mktime(0,0,0,$mon,1,$year));
	
$today=date("Y-m-j");

$d = 1;
$wday = $firstwday;
$firstweek = true;

while ( $d <= $lastday) 
{
		
	// Affichage des jours de la premi&egrave;re semaine
	if ($firstweek) {
			if($wday!="0"){ echo "<tr align=center>"; }
			for ($i=1; $i<=$firstwday; $i++) {
				echo "<td class='text10'>&nbsp;</td>";
			}
			$firstweek = false;
	}
		
	if ($wday==0) {
			// Ouverture de la ligne les dimanches
			echo "<tr align=center>";
	}
	// Affichage des jours
	// Select todo action tasks
	$str ="";	
	$sql = "SELECT DISTINCT id FROM prets WHERE date_int is null and date_out  = '$year-$mon-$d' " ;	        
	$result = biblio_db_query($sql);
	$i=biblio_db_num_rows($result);
	if ($i > 1) { $str="<FONT COLOR=RED>&nbsp;Sorties : $i&nbsp;</FONT>"; }
	elseif ($i < 1) {$str = $str . "&nbsp;";}
	else { $str = "<FONT COLOR=RED>&nbsp;Sortie : $i&nbsp;</FONT>"; }
	
	$sql = "SELECT DISTINCT id FROM prets WHERE date_int = '$year-$mon-$d' " ;	
	$result = biblio_db_query($sql);
	$i=biblio_db_num_rows($result);
	if ($i > 1) { $str = $str . "<BR><FONT COLOR=DarkOrange>&nbsp;Retours : $i&nbsp;</FONT>"; }
	elseif ($i < 1) {$str = $str . "&nbsp;";}
	else { $str = $str . "<BR><FONT COLOR=DarkOrange>&nbsp;Retour : $i&nbsp;</FONT>"; }

        $sql = "SELECT id FROM eleves WHERE newdate = '$year-$mon-$d' " ;	
	$result = biblio_db_query($sql);
	$i=biblio_db_num_rows($result);
	if ($i > 1) { $str = $str . "<BR><FONT COLOR=DarkGreen>&nbsp;" ._("Nv.El&egrave;ves")." : $i&nbsp;</FONT>"; }
	elseif ($i < 1) {$str = $str . "&nbsp;";}
	else { $str = $str . "<BR><FONT COLOR=DarkGreen>&nbsp;". _("Nv.El&egrave;ve") ." : $i&nbsp;</FONT>"; }

        $sql = "SELECT id FROM livres WHERE date_in = '$year-$mon-$d' " ;	
	$result = biblio_db_query($sql);
	$i=biblio_db_num_rows($result);
	if ($i > 1)
		$str .= "<BR><FONT COLOR=DarkBlue>&nbsp;". _("Nv.livres")." : $i&nbsp;</FONT>";
	elseif ($i < 1)
		$str .= "&nbsp;";
	else
		$str .= "<BR><FONT COLOR=DarkBlue>&nbsp;". _("Nv.livre"). " : $i&nbsp;</FONT>";
	echo "<td class='text10'><A HREF=\"".$_SERVER['PHP_SELF'].'?menu=1&date_to='.$year.'-'.$mon.'-'.$d.'">';
	echo '<B>'.$d.'</B></A><BR><BR>'.$str.'</td>'; 
				
	// Fini la semaine avec des espaces si le mois ne se termine pas par un samedi 
	$reste = 5-$wday;
	if($d==$lastday && $wday!=6){ 
		$i=0;
		while($i<=$reste){
			echo "\n<td class='text10'>&nbsp;</td>";
			$i++;
		}
	}
		
	if ($wday==6) {
		// Fermeture de la ligne les samedis
		echo "</tr>\n";
	}
		
	$wday++;
	$wday = $wday % 7;
	$d++;
}
?>
<tr class="text10bold" align="center" bgcolor=CCFFCC text=BLUE>
<td colspan="3" align=right>
<?php
// Affichage des links vers les mois pr&eacute;c&eacute;dents et suivants
if($mon==1){ $mon1="12"; $year1=$year-1; }
else{ $mon1=$mon-1; $year1=$year; }
echo "<a href='calendrier.php?mon=".$mon1."&year=".$year1."'>".$mois[$mon1]."</a>";
?>
</td><td align=center>-</td><td colspan="3" align=left>
<?php	
if($mon==12){ $mon2="1"; $year2=$year+1; }
else{ $mon2=$mon+1; $year2=$year; }
echo "<a href='calendrier.php?mon=".$mon2."&year=".$year2."'>".$mois[$mon2]."</a>";
?>
</td></tr><tr class="text10bold" align="center" bgcolor=CCFFCC><td colspan="3" align=right>
<?php
$year3=$year-1;
echo "<a href='calendrier.php?mon=".$mon."&year=".$year3."'>".$year3."</a>";
?>
</td><td align=center>-</td><td colspan="3" align=left>
<?php
$year4 = $year+1; 
echo "<a href='calendrier.php?mon=".$mon."&year=".$year4."'>".$year4."</a>";
?>
</td>
</tr>
</table>
<HR>
<CENTER>
<?php
ereg("(....)-(.*)-(.*)$", $date_to, $match);

$mon =sprintf("%d",$match[2]);
$date_to_str = "$match[3] $mois[$mon] $match[1]";
$requete = "SELECT t2.nom, t1.id id_to, t4.nom nom_el, t4.prenom prenom_el, t6.classe, t5.local, t5.biblio, t5.rangee FROM prets as t1 inner join livres as t2 on t2.id=t1.livre_ID inner join eleves as t4 on t4.id = t1.eleve_ID inner join classes as t6 on t6.id = t4.classe_id inner join locations as t5 on t5.id=t2.location_ID where date_int is null and date_out  = '". $date_to ."' order by t6.classe,t5.local, t5.biblio, t5.rangee, t4.nom, t2.nom "; 

if(!($resultat = biblio_db_query($requete)))
{ print "PROBLEME : " .biblio_db_error(); }
else 
{ 
  if(biblio_db_num_rows($resultat) > 0)
  {  
        $i=0;	        
        echo "<H3>Livres sortis de la biblioth&egrave;que le $date_to_str</H3><TABLE BORDER=1><tr><th>";
	echo _("Classe");
	echo "</th><th>";
	echo _("Eleve");
	echo "</th><th>";
	echo _("Titre");
	echo "</th><th>";
	echo _("Local");
	echo "</th><th>";
	echo _("Biblio");
	echo "</th><th>";
	echo _("Rang&eacute;e");
	echo "</TD>";
        while ($myrow = biblio_db_fetch_array($resultat)) 
        {
                $classe = $myrow['classe'];
                $eleve = $myrow['nom_el'] . " " . $myrow['prenom_el'];
                $local  = $myrow['local'];
                $biblio = $myrow['biblio'];
                $rangee = $myrow['rangee'];
                $livre_titre = $myrow['nom'];
                $i++;
                if ($i%2 == 1) {$col="WHITE";}
                else {$col="lightyellow";}
                echo "<TR bgcolor=$col><TD>". $classe ."</TD><TD>" .$eleve ."</TD><TD>". $livre_titre ."</TD><TD>". $local ."</TD><TD>". $biblio ."</TD><TD>". $rangee ."</TD>";      
        }
        echo "</TABLE><BR><HR>";
  }             
}
$requete = "SELECT DISTINCT t2.nom, t1.id id_to, t4.nom nom_el, t4.prenom prenom_el, t6.classe, t5.local, t5.biblio, t5.rangee FROM prets as t1 inner join livres as t2 on t2.id=t1.livre_ID inner join eleves as t4 on t4.id = t1.eleve_ID inner join classes as t6 on t6.id = t4.classe_id inner join locations as t5 on t5.id=t2.location_ID where date_int = '". $date_to ."' order by t6.classe,t5.local, t5.biblio, t5.rangee, t4.nom, t2.nom "; 
if(!($resultat = biblio_db_query($requete)))
{ print "PROBLEME : " .biblio_db_error(); }
else 
{ 
  if(biblio_db_num_rows($resultat) > 0)
  {  
        $i=0;	        
        echo "<H3>";
	echo _("Livres retourn&eacute;s &agrave; la biblioth&egrave;que le ");
	echo "$date_to_str</H3><TABLE BORDER=1><tr><th>";
	echo _("Classe");
	echo "</th><th>";
	echo _("Eleve");
	echo "</th><th>";
	echo _("Titre");
	echo "</th><th>";
	echo _("Local");
	echo "</th><th>";
	echo _("Biblio");
	echo "</th><th>";
	echo _("Rang&eacute;e");
	echo "</th></tr>";
        while ($myrow = biblio_db_fetch_array($resultat)) 
        {
                $classe = $myrow['classe'];
                $eleve = $myrow['nom_el'] . " " . $myrow['prenom_el'];
                $local  = $myrow['local'];
                $biblio = $myrow['biblio'];
                $rangee = $myrow['rangee'];
                $livre_titre = $myrow['nom'];
                $i++;
                if ($i%2 == 1) {$col="WHITE";}
                else {$col="lightyellow";}
                echo "<TR bgcolor=$col><TD>". $classe ."</TD><TD>" .$eleve ."</TD><TD>". $livre_titre ."</TD><TD>". $local ."</TD><TD>". $biblio ."</TD><TD>". $rangee ."</TD>";      
        }
        echo "</TABLE><BR><HR>";
  }             
}
$requete = "SELECT DISTINCT t1.nom, t2.local, t2.biblio, t2.rangee FROM livres as t1 inner join locations as t2 on t2.id=t1.location_ID where date_in = '". $date_to ."' order by t2.local, t2.biblio, t2.rangee, t1.nom "; 
if(!($resultat = biblio_db_query($requete)))
{ print "PROBLEME : " .biblio_db_error(); }
else 
{ 
  if(biblio_db_num_rows($resultat) > 0)
  {  
        $i=0;	        
        echo "<H3>";
	echo _("Livres ajout&eacute; &agrave; la base de donn&eacute;es le ");
	echo "$date_to_str</H3><TABLE BORDER=1><tr><th>";
	echo _("Titre");
	echo "</th><th>";
	echo _("Local");
	echo "</th><th>";
	echo _("Biblio");
	echo "</th><th>";
	echo _("Rang&eacute;e");
	echo "</th></tr>";
        while ($myrow = biblio_db_fetch_array($resultat)) 
        {
                $local  = $myrow['local'];
                $biblio = $myrow['biblio'];
                $rangee = $myrow['rangee'];
                $livre_titre = $myrow['nom'];
                $i++;
                if ($i%2 == 1) {$col="WHITE";}
                else {$col="lightyellow";}
                echo "<TR bgcolor=$col><TD>". $livre_titre ."</TD><TD>". $local ."</TD><TD>". $biblio ."</TD><TD>". $rangee ."</TD>";      
        }
        echo "</TABLE><BR><HR>";
  }             
}
$requete = "SELECT DISTINCT nom, prenom, t2.classe FROM eleves as t1 inner join classes as t2 on t2.id = t1.classe_id where newdate = '". $date_to ."' order by t2.classe, t1.nom, t1.prenom "; 
if(!($resultat = biblio_db_query($requete)))
{ print "PROBLEME : " .biblio_db_error(); }
else 
{ 
  if(biblio_db_num_rows($resultat) > 0)
  {  
        $i=0;	        
        echo "<H3>";
	echo _("El&egrave;ves ajout&eacute;s &agrave; la base de donn&eacute;es le ");
	echo "$date_to_str</H3><TABLE BORDER=1><tr><th>";
	echo _("Classe");
	echo "</th><th>";
	echo _("Nom");
	echo "</th><th>";
	echo _("Pr&eacute;nom");
	echo "</th></tr>";
        while ($myrow = biblio_db_fetch_array($resultat)) 
        {
                $classe = $myrow['classe'];
                $nom = $myrow['nom'];
                $prenom = $myrow['prenom'];                
                $i++;
                if ($i%2 == 1) {$col="WHITE";}
                else {$col="lightyellow";}
                echo "<TR bgcolor=$col><TD>". $classe ."</TD><TD>" .$nom ."</TD><TD>". $prenom ."</TD>";      
        }
        echo "</TABLE><FORM ACTION=\"printelcal.php\" METHOD=\"POST\">";
        echo "<INPUT TYPE=\"HIDDEN\" name=\"date_to\" value=\"$date_to\">";
        echo "<INPUT TYPE=\"submit\" name=\"submit\" value=\"";
	echo _("Imprimer carte(s)");
	echo "\"></FORM><BR><HR>";
  }             
}
?>
</center>
<br><br>
<div>
<span id=left-text>
<A HREF="admin.php?menu=1">
<?php echo _("retour menu administration"); ?>
</A><BR>
<A HREF="index.php">
<?php echo _("retour menu principal"); ?>
</A></span>
</div>
<br>
<?php
include("includes/footer.inc");
?>
